package com.sip.ruleevaluateadapter.comparator;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.sip.dbadapter.dao.domain.ApiDefinition;
import com.sip.ruleevaluateadapter.DomainComparator;

public class ApiDefinitionDomainComparator implements DomainComparator<Set<ApiDefinition>> {

    private Set<ApiDefinition> scannedApiDefinitions = null;

    public ApiDefinitionDomainComparator(final Set<ApiDefinition> scannedSet) {

        scannedApiDefinitions = scannedSet;
    }



    @Override
    public Set<ApiDefinition> compareUpdate(final Set<ApiDefinition> originalSet) {

   
        System.out.println("******orginal Api def length=================>"+originalSet.size() );
        System.out.println("****** scannedApiDefinitions length==========>"+scannedApiDefinitions.size() );
      
    
        //Delete  existing  API (Update- status to inactive)

        if (originalSet.size() > scannedApiDefinitions.size()) {

            // removing deleted
            for (final ApiDefinition apiDefinitionOld : originalSet) {

                Boolean isFound = Boolean.FALSE;

                for (final ApiDefinition apiDefinitionNew : scannedApiDefinitions) {

                    // isFound =
                    // apiDefinitionOld.getScanTypeRest().getMethodUrl().equals(apiDefinitionNew.getScanTypeRest().getMethodUrl());
                    isFound = apiDefinitionOld.getPackageHierachy().equals(apiDefinitionNew.getPackageHierachy());

                    if (isFound) {
                        System.out.println("******Api id found**************======>" + apiDefinitionOld.getApiId());
                        break;
                    }
                }

                if (!isFound) {
                    // update status to inactive
                    apiDefinitionOld.setApiStatus("INACTIVE");
                    originalSet.add(apiDefinitionOld);

                }

            }

        }
        
        
        // adding new API definitions
        for (final ApiDefinition apiDefinitionNew : scannedApiDefinitions) {

            Boolean isFound = Boolean.FALSE;

            for (final ApiDefinition apiDefinitionOld : originalSet) {
                // isFound =
                // apiDefinitionNew.getScanTypeRest().getMethodUrl().equals(apiDefinitionOld.getScanTypeRest().getMethodUrl());
                isFound = apiDefinitionNew.getPackageHierachy().equals(apiDefinitionOld.getPackageHierachy());
                if (isFound) {
                    System.out.println("******Api id found==####################======>" + apiDefinitionOld.getApiId());
                    apiDefinitionOld.setApiStatus("ACTIVE");
                    break;
                }
            }
            if (!isFound) {
                System.out.println("******Api id is Not found====xxxxxxxxxxxxxxx======>" + apiDefinitionNew.getApiId());
                originalSet.add(apiDefinitionNew);
            }

        }

   
        return originalSet;
    }

}
